package org.example.curriculum_design_6_16.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.example.curriculum_design_6_16.entity.pojo.ScholarshipCandidate;
import org.example.curriculum_design_6_16.entity.vo.ScholarshipRankingVo;
import org.example.curriculum_design_6_16.service.ScholarshipCandidateService;
import org.example.curriculum_design_6_16.mapper.ScholarshipCandidateMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author 郑凯
* @description 针对表【scholarship_candidate】的数据库操作Service实现
* @createDate 2025-06-16 09:26:01
*/
@Slf4j
@Service
public class ScholarshipCandidateServiceImpl extends ServiceImpl<ScholarshipCandidateMapper, ScholarshipCandidate>
    implements ScholarshipCandidateService{

    @Autowired
    private ScholarshipCandidateMapper scholarshipCandidateMapper;

    @Override
    @Transactional
    public void calculateScholarshipRanking(Integer grade, String academicYear, String scholarshipType) {
        try {
            log.info("开始计算{}年级{}学年{}奖学金排名", grade, academicYear, scholarshipType);
            scholarshipCandidateMapper.calculateScholarshipRanking(grade, academicYear, scholarshipType);
            log.info("{}年级{}学年{}奖学金排名计算完成", grade, academicYear, scholarshipType);
        } catch (Exception e) {
            log.error("计算奖学金排名失败 - 年级: {}, 学年: {}, 类型: {}", grade, academicYear, scholarshipType, e);
            throw new RuntimeException("计算奖学金排名失败: " + e.getMessage());
        }
    }

    @Override
    @Transactional
    public void calculateAllScholarshipRankings(String academicYear, String scholarshipType) {
        try {
            log.info("开始计算所有年级{}学年{}奖学金排名", academicYear, scholarshipType);
            scholarshipCandidateMapper.calculateAllScholarshipRankings(academicYear, scholarshipType);
            log.info("所有年级{}学年{}奖学金排名计算完成", academicYear, scholarshipType);
        } catch (Exception e) {
            log.error("计算所有年级奖学金排名失败 - 学年: {}, 类型: {}", academicYear, scholarshipType, e);
            throw new RuntimeException("计算所有年级奖学金排名失败: " + e.getMessage());
        }
    }

    @Override
    public List<ScholarshipRankingVo> getScholarshipRanking(Integer grade, String academicYear, String scholarshipType) {
        try {
            log.info("获取{}年级{}学年{}奖学金排名", grade, academicYear, scholarshipType);
            List<ScholarshipRankingVo> rankings = scholarshipCandidateMapper.getScholarshipRanking(grade, academicYear, scholarshipType);
            log.info("获取到{}条排名记录", rankings.size());
            return rankings;
        } catch (Exception e) {
            log.error("获取奖学金排名失败 - 年级: {}, 学年: {}, 类型: {}", grade, academicYear, scholarshipType, e);
            throw new RuntimeException("获取奖学金排名失败: " + e.getMessage());
        }
    }

    @Override
    public ScholarshipCandidate getStudentRanking(String studentId, String academicYear, String scholarshipType) {
        try {
            log.info("获取学生{}的{}学年{}奖学金排名", studentId, academicYear, scholarshipType);
            ScholarshipCandidate ranking = scholarshipCandidateMapper.getStudentRanking(studentId, academicYear, scholarshipType);
            if (ranking == null) {
                log.warn("学生{}在{}学年{}奖学金中无排名记录", studentId, academicYear, scholarshipType);
            }
            return ranking;
        } catch (Exception e) {
            log.error("获取学生排名失败 - 学生ID: {}, 学年: {}, 类型: {}", studentId, academicYear, scholarshipType, e);
            throw new RuntimeException("获取学生排名失败: " + e.getMessage());
        }
    }

    @Override
    public Map<String, Object> getRankingStatistics(String academicYear, String scholarshipType) {
        try {
            log.info("获取{}学年{}奖学金排名统计信息", academicYear, scholarshipType);
            
            Map<String, Object> statistics = new HashMap<>();
            
            // 获取研二排名
            List<ScholarshipRankingVo> grade2Rankings = getScholarshipRanking(2, academicYear, scholarshipType);
            statistics.put("grade2Rankings", grade2Rankings);
            statistics.put("grade2Count", grade2Rankings.size());
            
            // 获取研三排名
            List<ScholarshipRankingVo> grade3Rankings = getScholarshipRanking(3, academicYear, scholarshipType);
            statistics.put("grade3Rankings", grade3Rankings);
            statistics.put("grade3Count", grade3Rankings.size());
            
            // 计算总分统计
            if (!grade2Rankings.isEmpty()) {
                double grade2MaxScore = grade2Rankings.get(0).getTotalScore();
                double grade2MinScore = grade2Rankings.get(grade2Rankings.size() - 1).getTotalScore();
                double grade2AvgScore = grade2Rankings.stream().mapToDouble(ScholarshipRankingVo::getTotalScore).average().orElse(0.0);
                
                statistics.put("grade2MaxScore", grade2MaxScore);
                statistics.put("grade2MinScore", grade2MinScore);
                statistics.put("grade2AvgScore", grade2AvgScore);
            }
            
            if (!grade3Rankings.isEmpty()) {
                double grade3MaxScore = grade3Rankings.get(0).getTotalScore();
                double grade3MinScore = grade3Rankings.get(grade3Rankings.size() - 1).getTotalScore();
                double grade3AvgScore = grade3Rankings.stream().mapToDouble(ScholarshipRankingVo::getTotalScore).average().orElse(0.0);
                
                statistics.put("grade3MaxScore", grade3MaxScore);
                statistics.put("grade3MinScore", grade3MinScore);
                statistics.put("grade3AvgScore", grade3AvgScore);
            }
            
            log.info("统计信息获取完成");
            return statistics;
        } catch (Exception e) {
            log.error("获取排名统计信息失败 - 学年: {}, 类型: {}", academicYear, scholarshipType, e);
            throw new RuntimeException("获取排名统计信息失败: " + e.getMessage());
        }
    }
}




